import 'dart:async';

import 'package:manager/services/plugin_service.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';
import 'package:get/get_navigation/src/routes/default_transitions.dart';
import './res/color.dart';
import './services/preference_service.dart';

import 'configs/route_config.dart';

void main() async {
  // runZonedGuarded(() {
    WidgetsFlutterBinding.ensureInitialized();
    runApp(GetMaterialApp(
      theme: ThemeData(
          colorScheme: const ColorScheme(
              brightness: Brightness.light,
              primary: ColorPool.theme_color,
              onPrimary: Colors.white,
              secondary: ColorPool.button_disable,
              onSecondary: ColorPool.button_disable,
              error: Color(0xFF601410),
              onError: Colors.white,
              surface: Colors.white,
              onSurface: Colors.black87),
          primaryColor: ColorPool.theme_color,
          primaryColorDark: ColorPool.theme_color,
          primaryColorLight: ColorPool.theme_color,
          scaffoldBackgroundColor: Colors.white,
          unselectedWidgetColor: Colors.grey,
          dialogBackgroundColor: Colors.white,
          buttonTheme: ButtonThemeData(
              buttonColor: ColorPool.button_enable,
              disabledColor: ColorPool.button_disable,
              height: 45,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10)),
              highlightColor: ColorPool.button_pressed,
              hoverColor: ColorPool.button_pressed),
          dialogTheme: DialogTheme(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12)),
              actionsPadding: const EdgeInsets.only(bottom: 6, right: 6)),
          progressIndicatorTheme:
              const ProgressIndicatorThemeData(color: ColorPool.theme_color),
          floatingActionButtonTheme: FloatingActionButtonThemeData(
              backgroundColor: ColorPool.theme_color,
              foregroundColor: Colors.white,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(36))),
          tabBarTheme: const TabBarTheme(
              overlayColor: WidgetStatePropertyAll(Colors.white),
              labelColor: ColorPool.theme_color,
              indicatorColor: ColorPool.theme_color,
              unselectedLabelColor: ColorPool.text_common),
          switchTheme: const SwitchThemeData(
            thumbColor: WidgetStatePropertyAll(Colors.white),
            trackOutlineWidth: WidgetStatePropertyAll(0),
            // trackColor: WidgetStatePropertyAll(ColorPool.theme_color)
          ),
          iconTheme: const IconThemeData(color: Colors.white, size: 24),
          checkboxTheme: const CheckboxThemeData(
              checkColor: WidgetStatePropertyAll(Colors.white)),
          bottomNavigationBarTheme: const BottomNavigationBarThemeData(
              selectedItemColor: ColorPool.theme_color,
              unselectedItemColor: Colors.grey),
          appBarTheme: const AppBarTheme(
              actionsIconTheme: IconThemeData(color: Colors.white),
              foregroundColor: Colors.white,
              backgroundColor: ColorPool.theme_color,
              titleTextStyle: TextStyle(fontSize: 16, color: Colors.white))),
      builder: BotToastInit(),
      title: '幽兔进货',
      initialRoute: RouteConfig.main,
      themeMode: ThemeMode.light,
      getPages: RouteConfig.pages,
      navigatorObservers: [BotToastNavigatorObserver()],
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate
      ],
      supportedLocales: const [Locale('zh', 'CN')],
    ));

    setStatusBar();
    initServices();
  // }, (obj, stack) {
  //   // toErrorPage('发生错误：\n$obj\n\n$stack');
  // });
}

///设置沉浸式状态栏颜色
void setStatusBar() {
  var style = const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent, statusBarIconBrightness: Brightness.dark);
  SystemChrome.setSystemUIOverlayStyle(style);
}

///初始化常用service
void initServices() async {
  await Get.putAsync<PreferenceService>(() => PreferenceService().init(),
      permanent: true);
  await Get.putAsync<PluginService>(() => PluginService().init(),
      permanent: true);
}
